package com.VolunteerPlatform.Controller.volunteer;

import com.VolunteerPlatform.Entity.BankInfo;
import com.VolunteerPlatform.Entity.VolunteerInfo;
import com.VolunteerPlatform.Service.VolunteerService.BankInfoService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by DoveXL on 2018/12/7.
 */
@Controller
@RequestMapping(value = "/BankInfo/**")
public class BankInfoController {


    @Autowired
    private BankInfoService bankInfoService;

    @RequestMapping(value = "CertainBankInfo", method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String getBankInfo(@RequestBody String jsondata, HttpSession httpSession)
    {
        int rowCount=0;
        SecurityContextImpl securityContextImpl = (SecurityContextImpl) httpSession.getAttribute("SPRING_SECURITY_CONTEXT");
        String User = securityContextImpl.getAuthentication().getName();
        JSONObject jsonObject = JSON.parseObject(jsondata);
        int page = jsonObject.getInteger("page");
        int pagesize = jsonObject.getIntValue("pagesize");
        String IDCard = jsonObject.getString("IDCard");
        String Name = jsonObject.getString("Name");
        String Unit = jsonObject.getString("Unit");
        String BankAccount = jsonObject.getString("BankAccount");
        String BankBranch = jsonObject.getString("BankBranch");
        JSONArray UserInfoArray = new JSONArray();
        List<BankInfo> bankInfoList = bankInfoService.getBankInfoService(Unit,IDCard,Name,BankAccount,BankBranch,0,-1,-1);
        try {
            rowCount = bankInfoList.size();
            if(rowCount%pagesize!=0)
            {
                rowCount = rowCount / pagesize + 1;
            }
            else
            {
                rowCount = rowCount / pagesize;
            }
            List<BankInfo> VolunteerInfoList = bankInfoService.getBankInfoService(Unit,IDCard,Name,BankAccount,BankBranch,0,(page-1)*pagesize,pagesize);

            for (int i=0;i<VolunteerInfoList.size();i++) {
                JSONObject UserInfo = new JSONObject();
                UserInfo.put("user_name", VolunteerInfoList.get(i).getName());
                UserInfo.put("user_bankbranch", VolunteerInfoList.get(i).getBankBranch());
                UserInfo.put("user_IDCard", VolunteerInfoList.get(i).getIDCardNumber());
                UserInfo.put("user_bankaccount", VolunteerInfoList.get(i).getBankAccount());
                UserInfoArray.add(UserInfo);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("pagecount",rowCount);
        jsonObject1.put("currentpage",page);
        jsonObject1.put("pagelist",UserInfoArray);
        return JSON.toJSONString(jsonObject1);
    }


    @RequestMapping(value = "ChangeBankInfo", method = RequestMethod.POST)
    @ResponseBody
    public String getBankInfo(@RequestBody String jsondata)
    {
        JSONObject jsonObject = JSON.parseObject(jsondata);
        String IDCard = jsonObject.getString("IDCardNumber");
        String Name = jsonObject.getString("Name");
        String BankAccount = jsonObject.getString("BankAccount");
        String BankBranch = jsonObject.getString("BankBranch");
        int flag = bankInfoService.updateBankInfoService(IDCard,Name,BankAccount,BankBranch);
        System.out.println(flag);
        if(flag==1)
        {
            return "true";
        }else{
            return "false";
        }
    }



    @RequestMapping(value = "ExportBankInfo", method = RequestMethod.POST)
    @ResponseBody
    public String ExportBankInfo(@RequestBody String jsondata,HttpSession httpSession) throws IOException {

        JSONObject jsonObject = JSON.parseObject(jsondata);
        String checkFieldName = jsonObject.getString("checkFieldName");
        String Name = jsonObject.getString("Name");
        String IDCardNumber = jsonObject.getString("IDCardNumber");
        String Unit = jsonObject.getString("Unit");
        List<String> checkFieldNameList = JSON.parseArray(checkFieldName,String.class);
        List<String> checkFieldChineseNameList = new ArrayList<>();
        Map<String,String> TeamInfoChineseMap = new HashMap<>();
        TeamInfoChineseMap.put("Number","序号");
        TeamInfoChineseMap.put("Unit","单位");
        TeamInfoChineseMap.put("Name","姓名");
        TeamInfoChineseMap.put("IDCardNumber","身份证号");
        TeamInfoChineseMap.put("BankAccount","银行账户");
        TeamInfoChineseMap.put("BankBranch","开户银行");
        String title = "志愿者银行账户一览表";
        String[] rowsName = new String[checkFieldNameList.size()];
        for(int i =0;i<checkFieldNameList.size();i++)
        {
            checkFieldChineseNameList.add(TeamInfoChineseMap.get(checkFieldNameList.get(i)));
        }
        checkFieldChineseNameList.toArray(rowsName);
        int flag =0;
        String filePath = "D:\\code\\IntelIJ IDEA\\GeoLife\\LongHuaVolunteer\\target\\com.ecust\\files\\download\\";
        String fileName = "bankinfo"+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString() +".xls";
        flag =bankInfoService.ExportTeamInfoService(Name,IDCardNumber,Unit,title,rowsName,checkFieldNameList,filePath+fileName);
        httpSession.setAttribute("ExportBankInfoName",fileName);
        return String.valueOf(flag);
    }





















}
